package edu.hnui.shop.exception;

import edu.hnui.shop.commons.R;
import edu.hnui.shop.commons.ResultCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 进行全局的异常处理
 */

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 通用的异常处理
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();//输出异常栈的信息
        return R.error();
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public R error(NullPointerException e){
        e.printStackTrace();
        return R.setResult(ResultCodeEnum.NULL_POINT);
    }


    @ExceptionHandler(ShopBusinessException.class)
    @ResponseBody
    public R error(ShopBusinessException e){
        e.printStackTrace();
        return R.error().message(e.getMessage()).code(e.getCode());
    }
}
